import * as CONSTANTS from '@/config/constants.js';
import { formatDisplayData } from '@/utils/common.js';

const state = {
    feature: {
        name: '',
        displayName: '',
        dataName: '',
        unit: '',
        databaseField: '',
        iconClass: '',
        value: ''
    },
    oceanFeatureData: {
        current: '',
        temp: '',
        chlora: '',
        tide: '',
        wind: '',
        waveheight: ''
    }
}

// getters
const getters = {
    oceanFeatureList(state, getters, rootState) {
        return rootState.user.roleId == "1" ? CONSTANTS.AdminFeatureList : CONSTANTS.UserFeatureList;
    },
    featureDisplayValue(state) {
        const displayData = formatDisplayData(state.feature, state.feature.value);
        return state.feature.displayName + '：' + displayData.value + ' ' + displayData.unit;
    }
}

// actions
const actions = {
}

// mutations
const mutations = {
    setCurrentOceanFeature(state, value) {
        state.feature = { ...state.feature, ...value };
    },
    updateOceanFeatureData(state, value) {
        value.oceanDataList.forEach(data => {
            const feature = value.oceanFeatureList.find(feature => feature.displayName == data.name); 
            if (!feature) { return; }
            const displayData = formatDisplayData(feature, data.value);
            state.oceanFeatureData[feature.name] = displayData.value + ' ' + displayData.unit;
        })
    },
    updateCurrentOceanFeatureValue(state, value) {
        state.feature.value = value;
    }
}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}